

#include "pbm.h"

#include "xapp_ble.h"
#include "xapp_record.h"

#if 1 // Record 
int pi_record_is_empty(uint8_t *req, uint16_t len_of_req){
    return xapp_record_is_empty(); 
}

pbd_err_code_t pi_record_query(uint8_t *req, uint16_t len_of_req, uint8_t *pdata, uint16_t *p_size)
{
    if( req == NULL || len_of_req != 0)
    {
        return PBD_ERR_INVALID_LENGTH;
    }
    xapp_record_get_info(pdata, p_size);
    
    return PBD_SUCCESS; 
}
pbd_err_code_t pi_record_request_upload(uint8_t *req, uint16_t len_of_req, uint8_t *pdata, uint16_t *p_size)
{
    xapp_record_open_upload_session();


    if( xapp_record_get_data( 0xFFFFFFFF, pdata, p_size) > 0) {
        return PBD_ERR_INVALID_STATE; 
    }
    
    
    return PBD_SUCCESS; 
}
pbd_err_code_t pi_record_reception_ack(uint8_t *req, uint16_t len_of_req, uint8_t *pdata, uint16_t *p_size)
{
    if(req == NULL || len_of_req != 4){
        return PBD_ERR_INVALID_LENGTH;
    }

    uint32_t addr = uint32_decode( req );

    if( xapp_record_get_data( addr, pdata, p_size) > 0){
        return PBD_ERR_INVALID_STATE;
    }    

    return PBD_SUCCESS;
}
pbd_err_code_t pi_record_stop_session(uint8_t *pdata, uint16_t size)
{
    if( pdata == NULL || size != 0){
        return PBD_ERR_INVALID_LENGTH;
    }

    xapp_record_close_upload_session(); 
    return PBD_SUCCESS;
}
void pi_record_notify_data(uint32_t address, uint32_t size)
{
    pbm_record_notif_records_digest(address, size,  XAPP_BLE_PB_TX_HANDLER);
}

#endif // !Record 

void pbm_record_notif_records_digest(uint32_t addr, uint32_t size, pb_tx_array_handle_t cb)
{
    uint8_t data[11];

    data[0] = PBD_COMMAND_RECORD;

    data[1] = 9;
    data[2] = PBD_RECORD_KEY_RECORD_SIZE;

    data[3] = (uint8_t)(addr >> 0);
    data[4] = (uint8_t)(addr >> 8);
    data[5] = (uint8_t)(addr >> 16);
    data[6] = (uint8_t)(addr >> 24);
    data[7] = (uint8_t)(size >> 0);
    data[8] = (uint8_t)(size >> 8);
    data[9] = (uint8_t)(size >> 16);
    data[10] = (uint8_t)(size >> 24);

    pb_assemble_tx_data( data, 11, 0, cb); 
}


